package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanmao.blog.entity.Tag;
import com.lanmao.blog.service.TagService;
import com.lanmao.blog.utils.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    TagService tagService;

    @PostMapping("/saveTag")
    public R saveTag(@RequestBody Tag tag){
        if (StringUtils.isEmpty(tag.getName())){
            return R.error().message("标签名称不能为空！");
        }
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",tag.getName());
        List<Tag> tagList = tagService.list(queryWrapper);
        if (tagList.size() > 0){
            return R.error().message("添加失败，已有该标签名称");
        }
        Boolean result = tagService.save(tag);
        if (result){
            return R.ok().message("添加标签成功");
        }else {
            return R.error().message("添加标签失败");
        }

    }

    /**
     * 查询所有标签
     * @return
     */
    @GetMapping("/getAllTag")
    public R getAllTag(){
        List<Tag> list = tagService.list();
        return R.ok().data("data",list);
    }



    /**
     * 标签的分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getTagByPage")
    @ApiOperation("标签的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的数量"),
    })
    public R getTagByPage(Long current, Long size){
        Page<Tag> tagPage= tagService.pageTags(current, size);
        return R.ok().data("data",tagPage);
    }

    /**
     * 通过名称查找标签
     * @param name
     * @return
     */
    @GetMapping("/getTagByName")
    public R getTagByName(String name){
        List<Tag> tagList = tagService.list(new QueryWrapper<Tag>().like("name", name));
        return R.ok().data("data",tagList);
    }

    /**
     * 更新标签
     * @param tag
     * @return
     */
    @PutMapping("/updateTag")
    public R updateTag(@RequestBody Tag tag){
        if (StringUtils.isEmpty(tag.getName())){
            return R.error().message("分类名称不能为空");
        }
        boolean flag = tagService.updateById(tag);
        if (flag){
            return R.ok().message("更新标签成功");
        }
        return R.error().message("标签更新失败");
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @DeleteMapping("deleteTagById")
    public R deleteTagById(Long id){
        boolean flag = tagService.removeById(id);
        if (flag){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
     * 根据id查询标签
     * @param id
     * @return
     */
    @GetMapping("/getTagById")
    public R getTagById(Long id){
        Tag tag = tagService.getById(id);
        return R.ok().data("data",tag);
    }
}
